<?php
$profileURL = Common::getBaseUrl().'user/profile/';
$addFriendURL = Common::getBaseUrl().'user/addfriend/';
$deleteFriendURL = Common::getBaseUrl().'user/deletefriend/';
$loggedIn = Auth::hasIdentity();
$loggedInUser = Auth::getIdentity();
$friends = DataHelper::getFriendList();
$friendList = array();
// put all friends in a list for checking
if (!empty($friends)) {
	foreach ($friends as $friend) {
		$friendList[] = $friend['account_friend_id'];
	}
}
?>
<section>
	<h2>List of Users</h2>
	<?php echo Common::renderFlashMessage() ?>
	<?php 
		if ($this->allUsers):
		foreach($this->allUsers as $user): 
	?>
		<article class="users">
			<p>
			<div class="username"><a href="<?php echo $profileURL.$user['username']; ?>"><?php echo $user['username']; ?></a></div>
			<?php 
				if ($loggedIn):
				if ($user['account_id'] == $loggedInUser['account_id']):
			?>
			<div class="addfriend"><i>You</i></div>
			<?php 
				elseif (in_array($user['account_id'], $friendList)):
			?>
				<div class="friendsDelete"><a href="<?php echo $deleteFriendURL.$user['username']; ?>">Delete</a></div>
			<?php else: ?>
				<div class="friendsAdd"><a href="<?php echo $addFriendURL.$user['username']; ?>">Add</a></div>
			<?php 
				endif;
				endif; 
			?>
			</p>
		</article>
	<?php	
		endforeach;
		endif;
	?>
</section>